package _Pan3D.core {
	import _Pan3D.base.Camera3D;
	import _Pan3D.base.Object3D;

	// --------------MSN:lation_pan@live.cn  QQ: 3423526------------- //
	public class Groundposition {
		private static var _thisCam : Camera3D = new Camera3D({});
		private static var _vitem : Vector.<Object3D> = new Vector.<Object3D>;

		public function Groundposition() : void {
		}
		private static function _pushpoint(_obj : Object3D) : void {
			var _temp : Object3D = new Object3D();
			_temp.x = _obj.x;
			_temp.y = _obj.y;
			_temp.z = _obj.z;
			_temp.rx = 0;
			_temp.ry = 0;
			_temp.rz = 0;
			_vitem.push(_temp);
		}
		public static function _getposition(_sceneCam : Camera3D, f : Number, g : Number) : Object3D {
			_vitem = new Vector.<Object3D>;  //设定一个三角形，等会要跟据镜头的射线，来取得相交的点。（形容地面上的一个平面）
			_pushpoint(new Object3D(0,0, 500));
			_pushpoint(new Object3D(-500, 0, 0));
			_pushpoint(new Object3D(500, 0, 0));
			
			//复制镜头，因为这个新的镜头有别于原来;
			_thisCam=_sceneCam.clone();
			_thisCam.x = _sceneCam.x;
			_thisCam.y = _sceneCam.y;
			_thisCam.z = _sceneCam.z;
			//镜头角度，逆方向，这样才可得到新的旋转后的这些点的镜头坐标。（或说，镜头向左转30度，其实就可以说这些点向右转30度）
			_thisCam.angle_y = - _sceneCam.angle_y;
			_thisCam.angle_x = - _sceneCam.angle_x;
			_thisCam.sin_x = Math.sin(_thisCam.angle_x * Math.PI / 180);
			_thisCam.cos_x = Math.cos(_thisCam.angle_x * Math.PI / 180);
			_thisCam.sin_y = Math.sin(_thisCam.angle_y * Math.PI / 180);
			_thisCam.cos_y = Math.cos(_thisCam.angle_y * Math.PI / 180);
			
			for each (var object3D:Object3D in _vitem) {
				math_change_point(object3D);
			}
			return Calculation._get_hit_rec(_vitem[0], _vitem[1], _vitem[2], _sceneCam, f - _sceneCam.fovw / 2,  _sceneCam.fovh / 2-g);
		}
		public static function math_change_point(_3dpoint : Object3D) : void {
			//对坐标系里的原始点，跟据镜头角度算出新的从坐标
			var rx : Number = _3dpoint.x - _thisCam.x;
			var ry : Number = _3dpoint.y - _thisCam.y;
			var rz : Number = _3dpoint.z - _thisCam.z;
			
			//	var tmp_rx = this.rx;
			//	this.rx = int(Math.cos(tmp_angle)*tmp_rx-Math.sin(tmp_angle)*this.rz);
			//	this.rz = int(Math.sin(tmp_angle)*tmp_rx+Math.cos(tmp_angle)*this.rz);
			
			var tmp_rx : Number = rx;
			rx = _thisCam.cos_y * tmp_rx - _thisCam.sin_y * rz;
			rz = _thisCam.sin_y * tmp_rx + _thisCam.cos_y * rz;
			
			var tmp_rz : Number = rz;
			rz = _thisCam.cos_x * tmp_rz - _thisCam.sin_x * ry;
			ry = _thisCam.sin_x * tmp_rz + _thisCam.cos_x * ry;
			
			_3dpoint.rx = int(rx);
			_3dpoint.ry = int(ry);
			_3dpoint.rz = int(rz);
		}


	}
}